package com.fit4as.fit{

// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Released under the terms of the GNU General Public License version 2 or later.


import mx.utils.StringUtil;

public class PrimitiveFixture extends Fixture
{

    // format converters ////////////////////////

    public static function parseLong(cell : Parse) : Number {
        var result : Number = parseFloat(cell.text());
        if (result % 1 > 0) throw new Exception("NumberFormatException: For input string: \"" + cell.text() + "\"");
        return result;
    }

    public static function parseDouble(cell : Parse) : Number {
        return parseFloat(cell.text());
    }

    public static function parseBoolean(cell : Parse) : Boolean {
        return StringUtil.trim(cell.text()).toLowerCase() == "true";
    }

    // answer comparisons ///////////////////////


    override public function check(... rest) : void {
        var cell : Parse = Parse(rest[ 0 ]);
        var value : * = rest[ 1 ];

        if (value is String) checkString( cell, value);
        else if (isNaN(value)) checkNaN( cell, value);
        else if (value is int) checkLong( cell, value);
        else if (value is Number) checkDouble( cell, value);
        else if (value is Boolean) checkBoolean( cell, value);
        else throw new Error("Unexpected value: " + value);
    }


    public function checkString(cell : Parse, value : String) : void {
        if (cell.text() == value) {
            right(cell);
        } else {
            wrong(cell, value);
        }
    }

    public function checkNaN(cell : Parse, value : Number) : void {
        if (cell.text() == "NaN") {
            right(cell);
        } else {
            wrong(cell, value);
        }
    }

    public function checkLong(cell : Parse, value : int) : void {
        //t race("PrimitiveFixture#checkLong " + [parseLong(cell), value]);
        if (parseLong(cell) == value) {
            right(cell);
        } else {
            wrong(cell, value.toString());
        }
    }

    public function checkDouble(cell : Parse, value : Number) : void {
        if (parseDouble(cell) == value) {
            right(cell);
        } else {
            wrong(cell, value.toString());
        }
    }

    public function checkBoolean(cell : Parse, value : Boolean) : void {
        if (parseBoolean(cell) == value) {
            right(cell);
        } else {
            wrong(cell, value ? "true" : "false");
        }
    }

}
}